package common.plugins.pay;

import android.content.Context;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.orhanobut.logger.Logger;

import common.base.MainApplication;
import common.base.operationHelper.Interface.OkGoFinishListener;
import common.tool.JsonTool;
import es.dmoral.toasty.Toasty;

/**
 * description: 通过微信code获取access_token
 * author: tongzhenggang@126.com
 * date: 2018/3/3
 */
public class GetWeixinLoginData {
    //    http请求方式: GET  https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
    private String GetHttp = "https://api.weixin.qq.com/sns/oauth2/access_token";
    private String appid = MainApplication.APP_ID;
    private String secret = "e80e4940712e06bbe0e3c0a2b242d1d2";
    private String grant_type = "authorization_code";

    public GetWeixinLoginData(final OkGoFinishListener mListener, final Context mContext, String code) {

//        appid	是	应用唯一标识，在微信开放平台提交应用审核通过后获得
//        secret	是	应用密钥AppSecret，在微信开放平台提交应用审核通过后获得
//        code	是	填写第一步获取的code参数
//        grant_type	是	填authorization_code

        HttpParams params = new HttpParams();
        params.put("appid", appid);
        params.put("secret", secret);
        params.put("code", code);
        params.put("grant_type", grant_type);

        OkGo.<String>get(GetHttp)
                .tag(mContext)
                .params(params)

                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<String> response) {
                        Logger.d("code>>>" + response.code() + "  body>>>" + response.body().toString());

                        try {
                            ReturnOkBean bean = JsonTool.parseObject(response.body().toString().trim(), ReturnOkBean.class);//Gson转Bean
                            if (mListener != null) {
                                mListener.onSuccess("请求成功", bean);
                            }
                        } catch (Exception ex) {
                            Logger.e("onError>>>1001解析异常！ 提示详情>>>" + ex.getMessage());
                            Toasty.error(mContext, "onError>>>1001解析异常！ 提示详情>>>" + ex.getMessage()).show();
                        }
                    }

                    @Override
                    public void onError(com.lzy.okgo.model.Response<String> response) {
                        Logger.e("onError>>>" + response.message());
                        Toasty.error(mContext, "onError>>>" + response.message()).show();
                    }

                });
    }

    //正确的返回：
    public class ReturnOkBean {

        /**
         * access_token : 7_c8fyXFKglXDqfKVqeU586XZUh2ntaJs9Ct5h7ww_iMwserG59YJOdmX4lDssachJIZzIie7_QSsDilJt4I11-WhDKvAaVjow9LG1ZTLxR5Y
         * expires_in : 7200
         * refresh_token : 7_K6hbX7UTg1CzDVrNz-viFM03HaDBEE20fZTCHZKJT4m8lOcDTKIAYm9zeCzGK89fLTE3H6V_YWHcMe7V6rUOJpTt6xNFq4HN7tpnKIvuVGo
         * openid : oWXmj0zPF7WXJsu2zXH2q9inmJfc
         * scope : snsapi_userinfo
         * unionid : o1mDv1GoE-pTSu9WF6s9NI9mu7u8
         */

        private String access_token;
        private int expires_in;
        private String refresh_token;
        private String openid;
        private String scope;
        private String unionid;

        public String getAccess_token() {
            return access_token;
        }

        public void setAccess_token(String access_token) {
            this.access_token = access_token;
        }

        public int getExpires_in() {
            return expires_in;
        }

        public void setExpires_in(int expires_in) {
            this.expires_in = expires_in;
        }

        public String getRefresh_token() {
            return refresh_token;
        }

        public void setRefresh_token(String refresh_token) {
            this.refresh_token = refresh_token;
        }

        public String getOpenid() {
            return openid;
        }

        public void setOpenid(String openid) {
            this.openid = openid;
        }

        public String getScope() {
            return scope;
        }

        public void setScope(String scope) {
            this.scope = scope;
        }

        public String getUnionid() {
            return unionid;
        }

        public void setUnionid(String unionid) {
            this.unionid = unionid;
        }

        @Override
        public String toString() {
            return "ReturnOkBean{" +
                    "access_token='" + access_token + '\'' +
                    ", expires_in=" + expires_in +
                    ", refresh_token='" + refresh_token + '\'' +
                    ", openid='" + openid + '\'' +
                    ", scope='" + scope + '\'' +
                    ", unionid='" + unionid + '\'' +
                    '}';
        }
    }


}
